import { computed } from 'vue'
import { useEditStore } from '@/store'

// javascript代码提示
export default function useJavascriptCompletion(monaco) {
    const editStore = useEditStore()

    const appData = computed(() => editStore.appData)
    const pageData = computed(() => editStore.pageData)
    // 全局的普通变量/模型变量
    const appState = computed(() => appData.value?.data?.dataset?.state || {})
    // 当前页面的普通变量/模型变量
    const pageState = computed(() => pageData.value?.dataset?.state || {})
    // 当前页面的参数变量
    const pageParams = computed(() => pageData.value?.dataset?.params || {})

    const javascriptCompletion = monaco.languages.registerCompletionItemProvider('javascript', {
        triggerCharacters: [
            '.',
            ''
        ],
        provideCompletionItems: (model, position) => {
            const { column, lineNumber } = position
            // 光标前文本
            const textBeforePointer = model.getValueInRange({
                startLineNumber: lineNumber,
                startColumn: 0,
                endLineNumber: lineNumber,
                endColumn: column
            })

            let suggestions = []
            const { Function: kindFunction } = monaco.languages.CompletionItemKind
            const prototypeArr = [{
                'name': '$app',
                'detail': '全局变量',
                'kind': kindFunction,
                'attrs': [{
                    'name': 'dataset',
                    'detail': '全局变量的属性',
                    'kind': kindFunction,
                    'attrs': [{
                        'name': 'state',
                        'detail': '全局变量的普通变量/模型变量',
                        'kind': kindFunction,
                        'attrs': Object.keys(appState.value).map(key => {
                            const { varType, label } = appState.value[key]
                            return {
                                'name': key,
                                'detail': (varType === 'datasource' ? '模型变量属性' : '普通变量属性') + (!!label ? `(${label})` : ''),
                                'kind': kindFunction
                            }
                        })
                    }]
                }]
            }, {
                'name': '$page',
                'detail': '页面变量',
                'kind': kindFunction,
                'attrs': [{
                    'name': 'dataset',
                    'detail': '页面变量的属性',
                    'kind': kindFunction,
                    'attrs': [{
                        'name': 'state',
                        'detail': '页面变量的普通变量/模型变量',
                        'kind': kindFunction,
                        'attrs': Object.keys(pageState.value).map(key => {
                            const { varType, label } = pageState.value[key]
                            return {
                                'name': key,
                                'detail': (varType === 'datasource' ? '模型变量属性' : '普通变量属性') + (!!label ? `(${label})` : ''),
                                'kind': kindFunction
                            }
                        })
                    }, {
                        'name': 'params',
                        'detail': '页面变量的参数变量',
                        'kind': kindFunction,
                        'attrs': Object.keys(pageParams.value).map(key => {
                            const { label } = pageParams.value[key]
                            return {
                                'name': key,
                                'detail': '参数变量属性' + (!!label ? `(${label})` : ''),
                                'kind': kindFunction
                            }
                        })
                    }]
                }]
            }]

            const formatToSuggestions = arr => arr.map(n => ({
                label: n.name,
                insertText: n.name,
                detail: n.detail,
                kind: n.kind
            }))
            const forArr = (list, parentName = '') => {
                list.forEach(item => {
                    const name = parentName + item.name + '.'
                    if (name === textBeforePointer) suggestions.push(...formatToSuggestions(item.attrs))
                    forArr(item.attrs || [], name)
                })
            }
            forArr(prototypeArr)

            if (suggestions.length === 0) suggestions = formatToSuggestions(prototypeArr)

            return {
                suggestions
            }
        }
    })

    return {
        javascriptCompletion
    }
}
